#include "Functions.h"


Functions::Functions(void)
{
}


Functions::~Functions(void)
{
}

void Functions::swap_int(int *a, int *b) 
{  
	int tmp;
	tmp = *a;
	*a = *b;
	*b = tmp; 
}  

void Functions::getMeasurementPoint2(Point midPoint, Point pt1, Point pt2, Point&out1, Point&out2)
{
	float x = (float)(pt2.x - pt1.x);
	float y = (float)(pt2.y - pt1.y);
	float gen = 100.0 * 1.5 / (sqrt(x * x + y * y));
	out1.x = (int)(midPoint.x - x * gen);
	out1.y = (int)(midPoint.y - y * gen);
	out2.x = (int)(midPoint.x + x * gen);
	out2.y = (int)(midPoint.y + y * gen);
}

void Functions::getMeasurementPoint1(Point midPoint, Point pt1, Point pt2, Point&out1, Point&out2)
{
//	float x = (float)(pt1.x - pt2.x);
//	float y = (float)(pt1.y - pt2.y);
	float x = -(float)(pt1.y - pt2.y);
	float y = (float)(pt1.x - pt2.x);
	float gen = 20.0 * 1.5 / (sqrt(x * x + y * y));
	out1.x = (int)(midPoint.x - x * gen);
	out1.y = (int)(midPoint.y - y * gen);
	out2.x = (int)(midPoint.x + x * gen);
	out2.y = (int)(midPoint.y + y * gen);
}
// Bresenham's line algorithm  

//void Functions::draw_line(Mat *img, int x1, int y1, int x2, int y2, vector<Point>& points) 
void Functions::draw_line(int x1, int y1, int x2, int y2, vector<Point>& points) 
{  
	points.clear();
	int dx = abs(x2 - x1),  
		dy = abs(y2 - y1),  
		yy = 0;  
	if (dx < dy) {  
		yy = 1;  
		Functions::swap_int(&x1, &y1);  
		Functions::swap_int(&x2, &y2);  
		Functions::swap_int(&dx, &dy);  
	}  
	int ix = (x2 - x1) > 0 ? 1 : -1,  
		iy = (y2 - y1) > 0 ? 1 : -1,  
		cx = x1,  
		cy = y1,  
		n2dy = dy * 2,  
		n2dydx = (dy - dx) * 2,  
		d = dy * 2 - dx;  
	if (yy) 
	{ 
		while (cx != x2) 
		{  
			if (d < 0) 
			{  
				d += n2dy;  
			} 
			else 
			{  
				cy += iy;  
				d += n2dydx;  
			}  
//			circle(*img, Point(cx, cy), 0, (100, 100, 100));
//			cout<<cx<<"  "<<cy<<endl;
			points.push_back(Point(cy, cx));
			cx += ix;  
		}  
	} 
	else 
	{
		while (cx != x2) 
		{  
			if (d < 0) 
			{  
				d += n2dy;  
			} 
			else 
			{  
				cy += iy;  
				d += n2dydx;  
			}  
//			circle(*img, Point(cx, cy), 0, (100, 100, 100));
//			cout<<cx<<"  "<<cy<<endl;
			points.push_back(Point(cx,cy));
			cx += ix;  
		}  
	}  
}

string Functions::int2str( int num)
{
	if (num == 0 )
		return " 0.jpg " ;                                                                                                                                      

	string str = "" ;
	int num_ = num > 0 ? num : - 1 * num;

	while (num_)
	{
		str = ( char )(num_ % 10 + 48 ) + str;
		num_ /= 10 ;
	} 

	if (num < 0 )
	{
		str = " - " + str;
	}

	str	= str + ".jpg";

	return str;
}


double Functions::uniform_random(void)
{
	return (double) rand() / (double) RAND_MAX;
}
double Functions::gaussian_random(void)
{
	static int next_gaussian = 0;
	static double saved_gaussian_value;

	double fac, rsq, v1, v2;

	if (next_gaussian == 0) {
		do {
			v1 = 2.0*uniform_random()-1.0;
			v2 = 2.0*uniform_random()-1.0;
			rsq = v1*v1+v2*v2;
		} while (rsq >= 1.0 || rsq == 0.0);
		fac = sqrt(-2.0*log(rsq)/rsq);
		saved_gaussian_value=v1*fac;
		next_gaussian=1;
		//	cout<<(v2*fac);
		return v2*fac;
	} else {
		next_gaussian=0;
		//	cout<<saved_gaussian_value;
		return saved_gaussian_value;
	}
}